#!/usr/bin/env python3
"""
三相开关谜题求解器 - 打包脚本
将GUI程序打包成exe文件
"""

import subprocess
import os


def build_exe():
    """打包程序为exe文件"""
    print("🚀 开始打包三相开关谜题求解器...")
    print("=" * 50)

    # 检查必要文件
    required_files = [
        "switch_puzzle_gui.py",
        "switch_puzzle_solver.py",
        "puzzle-grid.ico",
    ]

    for file in required_files:
        if not os.path.exists(file):
            print(f"❌ 错误：找不到必要文件 {file}")
            return False

    print("✅ 所有必要文件检查完成")

    # PyInstaller命令
    cmd = [
        "pyinstaller",
        "--onefile",
        "--windowed",
        "--name=三相开关谜题求解器",
        "--icon=puzzle-grid.ico",
        "--add-data=README.md;.",
        "--add-data=GUI_README.md;.",
        "--hidden-import=numpy",
        "--hidden-import=tkinter",
        "--clean",
        "switch_puzzle_gui.py",
    ]

    print("📦 执行打包命令...")
    print(f"命令: {' '.join(cmd)}")
    print("-" * 50)

    try:
        result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8")

        if result.returncode == 0:
            print("✅ 打包成功！")
            print("📁 生成的文件位置：dist/三相开关谜题求解器.exe")
            return True
        else:
            print("❌ 打包失败！")
            print("错误输出：", result.stderr)
            return False

    except Exception as e:
        print(f"❌ 打包过程中出现错误：{e}")
        return False


if __name__ == "__main__":
    print("🔧 三相开关谜题求解器 - 打包工具")
    print("=" * 50)

    success = build_exe()

    if success:
        print("\n🎊 打包完成！")
    else:
        print("\n😞 打包失败，请检查错误信息")

    input("\n按回车键退出...")
